package leetcode.力扣竞赛.第388场周赛;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * description:
 *
 * @author 颜真明
 * @date 2024/3/10  20:19
 */
public class Solution100247 {
    public static void main(String[] args) {
        int[] arr = new int[]{12,1,42};
        System.out.println(maximumHappinessSum(arr,3));
    }
    public static long maximumHappinessSum(int[] happiness, int k) {
        long res = 0;
        List<Integer> list = new ArrayList<Integer>();
        for (int happy : happiness) {
            list.add(happy);
        }
        list.sort(Comparator.reverseOrder());
        while (k > 0) {
            if (list.get(0) > 0) {
                res += list.get(0);
            }
            list.remove(0);
            list = list.stream()
                    .map(num->{
                        num = num - 1;
                        if (num <= 0){num = 0;}
                        return num;
                    })
                    .collect(Collectors.toList());
            k--;
        }
        return res;
    }
}
